import CryptoJS from 'crypto-js';
// 服务端使用的加密密钥和IV
const secretKey = 'zkbaice-server-encryption-key-12345';
const secretIv = 'zkbaice-server-iv';

// 解密
export const decryptData = (encryptedData, secretKey="zkbaice-server-encryption-key-12345", secretIv="zkbaice-server-iv") => {
  // 确保密钥和IV长度正确
  const key = CryptoJS.SHA256(secretKey).toString();
  const iv = CryptoJS.enc.Utf8.parse(secretIv.substring(0, 16).padEnd(16, '0'));
  
  try {
    // 解密数据
    const decrypted = CryptoJS.AES.decrypt(
      encryptedData, 
      CryptoJS.enc.Hex.parse(key), 
      { 
        iv: iv, 
        mode: CryptoJS.mode.CBC, 
        padding: CryptoJS.pad.Pkcs7 
      }
    );
    
    // 转换为原始数据
    return JSON.parse(decrypted.toString(CryptoJS.enc.Utf8));
  } catch (error) {
    console.error('解密失败:', error);
    return null;
  }
}

// 解密数据
// const decryptedData = decryptData(encryptedData, secretKey, secretIv);
// console.log('解密后的数据:', decryptedData);